package com.cqc.paoclient.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.cqc.paoclient.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by cui on 2017/11/22.
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {

    protected P mPresenter;
    private Unbinder unbinder;
    protected Intent dataIntent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        dataIntent = getIntent();
        mPresenter = getmPresenter();
        presenterSetView();
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.removeView();
        }
    }

    public abstract int getLayoutId();

    public abstract P getmPresenter();

    public abstract void presenterSetView();

    public abstract void initView();

    public abstract void initData();

    protected void showShort(String msg) {
        ToastUtil.showShortToast(this, msg);
    }

    protected void showLong(String msg) {
        ToastUtil.showLongToast(this, msg);
    }

    public void stopRefresh(SmartRefreshLayout refreshLayout) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
    }

    public void stopLoadMore(SmartRefreshLayout refreshLayout) {
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
    }

}
